package com.example.shirodemo.controller.admin;

import cn.hutool.core.lang.Assert;
import com.example.shirodemo.constant.HttpCode;
import com.example.shirodemo.controller.vo.MenuVo;
import com.example.shirodemo.dto.ResponseResult;
import com.example.shirodemo.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author 绫小路
 * @Date 2021/1/11
 */
@Api(tags = "03-后台页面：菜单管理 - MenuController")
@Controller
@RequestMapping("/admin/menu")
public class MenuController {

  @Autowired
  private AdminService adminService;

  @ApiOperation("后台管理获取所有菜单")
  @GetMapping("all")
  @ResponseBody
  public ResponseResult all() {
    return adminService.getAllMenu();
  }

  @ApiOperation("获取菜单的详细信息，根据菜单id")
  @GetMapping("info")
  @ResponseBody
  public ResponseResult info(Integer id) {
    if (id == null) {
      return null;
    }
    return new ResponseResult(HttpCode.OK, adminService.menuInfo(id));
  }

  @ApiOperation("后台管理选择icon图标页面")
  @GetMapping("selectIcon")
  public String selectIcon() {
    return "admin/select-icon";
  }

  @ApiOperation("更新菜单")
  @PostMapping("update")
  @ResponseBody
  public ResponseResult update(@RequestBody MenuVo menuVo) {
    Assert.isFalse(menuVo.getId() == null, "入参Id不能为空！");
    Assert.isFalse(menuVo.getStatus() == null, "菜单状态不能为空！");
    Assert.isFalse(menuVo.getSortId() == null, "排序Id不能为空！");
    Assert.isFalse(menuVo.getSortId() > 999 || menuVo.getSortId() < 1, "排序Id不能大于999或者小于1");
    Assert.notBlank(menuVo.getHref(), "菜单URL不能为空！");
    Assert.notBlank(menuVo.getName(), "菜单名字不能为空！");
    Assert.notBlank(menuVo.getIcon(), "菜单图标不能为空！");
    return adminService.updateMenu(menuVo);
  }

  @ApiOperation("后台管理删除菜单")
  @PostMapping("delete")
  @ResponseBody
  public ResponseResult delete(Integer id){
    Assert.isFalse(id == null, "入参Id不能为空！");
    return adminService.deleteMenu(id);
  }

  @ApiOperation("后台管理添加菜单页面")
  @GetMapping("add")
  public String add() {
    return "admin/admin-menu-add";
  }

  @ApiOperation("添加菜单")
  @PostMapping("add")
  @ResponseBody
  public ResponseResult addPost(@RequestBody MenuVo menuVo) {
    Assert.isFalse(menuVo.getPid() == null, "请选择一个父菜单再添加！");
    Assert.isFalse(menuVo.getStatus() == null, "菜单状态不能为空！");
    Assert.isFalse(menuVo.getSortId() == null, "排序Id不能为空！");
    Assert.isFalse(menuVo.getSortId() > 999 || menuVo.getSortId() < 1, "排序Id不能大于999或者小于1");
    Assert.notBlank(menuVo.getHref(), "菜单URL不能为空！");
    Assert.notBlank(menuVo.getName(), "菜单名字不能为空！");
    Assert.notBlank(menuVo.getIcon(), "菜单图标不能为空！");
    return adminService.addMenu(menuVo);
  }

}
